package com.rjwh.dingdong.client.mvp.presenter.impl;

import android.content.Context;

import com.aishua.lib.task.AsResult;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.bean.jsonbean.ResGetRadioAblumListBean;
import com.rjwh.dingdong.client.bean.localbean.GetRadioAblumListBean;
import com.rjwh.dingdong.client.bean.localbean.RadioPageAlbumData;
import com.rjwh.dingdong.client.constant.NetConstant;
import com.rjwh.dingdong.client.mvp.BasePresenter;
import com.rjwh.dingdong.client.mvp.model.impl.RadioAlbumListModel;
import com.rjwh.dingdong.client.mvp.presenter.IRadioAlbumListPresenter;
import com.rjwh.dingdong.client.mvp.view.RadioAlbumListView;
import com.rjwh.dingdong.client.util.ToastUtil;

import java.util.List;

/**
 * @author Kay
 * @ClassName RadioAlbumListPresenter
 * @Description
 * @date 2016-11-16
 * @history 2016-11-16 author: description:
 */

public class RadioAlbumListPresenter extends BasePresenter<RadioAlbumListView> implements IRadioAlbumListPresenter {

    private RadioAlbumListModel model;
    private SwipyRefreshLayoutDirection mDirection;

    public RadioAlbumListPresenter(Context context, RadioAlbumListView mMvpView) {
        super(context, mMvpView);
        this.model = new RadioAlbumListModel(this);
    }

    @Override
    public void onServerDataAcquired(int businessType, AsResult mAsResult, boolean isRefresh) {
        if (isViewAttached()) {
            switch (businessType) {
                case NetConstant.GETRADIOABLUMLIST:
                    getMvpView().setRefreshing(false);
                    ResGetRadioAblumListBean resGetRadioAblumListBean = (ResGetRadioAblumListBean) mAsResult.getObj();
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS && resGetRadioAblumListBean != null) {
                        GetRadioAblumListBean data = resGetRadioAblumListBean.getData();
                        if (data != null) {
                            List<RadioPageAlbumData> albumlist = data.getAlbumlist();
                            if (albumlist != null) {
                                if (albumlist.isEmpty()) {
                                    getMvpView().setDirection(SwipyRefreshLayoutDirection.TOP);
                                    if (mDirection == SwipyRefreshLayoutDirection.BOTTOM) {
                                        ToastUtil.showToast(getContext(), getContext().getString(R.string.empty_data_more));
                                    } else {
                                        ToastUtil.showToast(getContext(), getContext().getString(R.string.empty_data));
                                    }
                                } else {
                                    getMvpView().setDirection(SwipyRefreshLayoutDirection.BOTH);
                                }
                                getMvpView().loadData(isRefresh, albumlist);
                            }
                        }
                    } else {
                        getMvpView().setDirection(SwipyRefreshLayoutDirection.TOP);
                        ToastUtil.showToast(getContext(), getContext().getString(R.string.refresh_error));
                    }
                    break;

                default:
                    break;
            }
        }
    }

    @Override
    public void detachView() {
        model.detachView();
        detachViews();
    }

    /**
     * 获取专辑列表
     *
     * @param category  专辑类型
     * @param pageindex 页码
     * @param direction
     */
    @Override
    public void doGetRadioAlbumList(String category, int pageindex, boolean isRefresh, SwipyRefreshLayoutDirection direction) {
        mDirection = direction;
        model.doGetRadioAlbumList(category, pageindex, isRefresh);
    }

}
